package Test_0814.ioperations;

import Test_0814.book.Book;
import Test_0814.book.BookShelf;

import java.util.Scanner;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 * Dare: 2024-08-15
 * Time: 9:35
 */
public class AddOperation implements IoPeration{
    public void work(BookShelf bookshelf) {
        System.out.println("新增图书");
        //判断书架是否已经被放满
        int ret=bookshelf.getUseSize();
        if(ret==bookshelf.getBooks().length){
            System.out.println("oh，该书已经放满");
        }
        //如果没有放满，输入信息
        System.out.println("请输入你要添加的书的信息：");
        System.out.println("请输入书名：");
        Scanner scanner = new Scanner(System.in);

        String name = scanner.nextLine();
        System.out.println("请输入作者姓名：");
        //判断书架中是否已有该本书
        for (int i = 0; i < ret; i++) {
            Book book1=bookshelf.getBook(i);
            if(book1.getBookName().equals(name)){
                System.out.println("书架中已有该书");
                return;
            }
        }
        String author = scanner.nextLine();


        System.out.println("请输入该书价格：");
        int price = scanner.nextInt();
        System.out.println("请输入该书类型：");
        String type = scanner.nextLine();
        Book book=new Book(name,author,price,type);

        //新增该书
        bookshelf.setBook(ret,book);
        bookshelf.setUseSize(ret+1);
        System.out.println("已新增该图书");
    }
}
